/* -----------------------------------------------------------------------------
 * PROJECT    : lwf_handlers
 * FILE       : ArithmaticWp.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Sep 21, 2011  mycodetalks			       Created
 */
package net.lwf.handler.wp.helper.general;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import lwf.handler.entities.RadioData;
import net.lwf.common.constant.ICommonConstant;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.wp.helper.WpImpl;
import net.lwf.jaxb.entity.BaseData;
import net.lwf.jaxb.service.XMLDataService;
import net.lwf.number.util.RandomNumberGenerator;

public class ArithmaticWp extends WpImpl {
	
	public BaseData getBaseData(int grade, String operation) {
		XMLDataService service = new XMLDataService();
		return service.getArithmaticData(grade, operation);
	}
	
	public Map<Integer, String[]> extractMultipleData(String asteriskcsvdata) {
		Map<Integer, String[]> data = null;
		if(asteriskcsvdata != null) {
			data = new HashMap<Integer, String[]>();
			StringTokenizer stk = new StringTokenizer(asteriskcsvdata, ICommonConstant.ASTERISK_DELIMETER);
			int nCounter = 1;
			while(stk.hasMoreTokens()) {
				data.put(new Integer(nCounter), extractData(stk.nextToken()));
				nCounter++;
			}
		}
		return data;
	}
	
	public String[] extractData(String csvdata) {
		String[] values = null;
		if(csvdata != null) {
			StringTokenizer stk = new StringTokenizer(csvdata, ICommonConstant.COMMA);
			values = new String[stk.countTokens()];
			int nCounter = 0;
			while(stk.hasMoreTokens()) {
				values[nCounter] = stk.nextToken();
				nCounter++;
			}
		}
		return values;
	}
	
	/**
	 * @param question
	 * @param replacePHs	Comma separated place holder names
	 * @param values
	 * @param answer
	 * @param hasDecimals
	 * @return
	 */
	public QuestionInfo setQuestionInfo(String question, String replacements, String[] values, String answer, boolean hasDecimals) {
		QuestionInfo qinfo = new QuestionInfo();
		qinfo.setAnswer(answer);
		
		//Replace the place holders
		if(replacements != null && replacements.trim().length() != 0) {
			StringTokenizer stkpHs = new StringTokenizer(replacements, ICommonConstant.COMMA);
			while(stkpHs.hasMoreTokens()) {
				question = replacePH(question, stkpHs.nextToken().trim());
			}
		}

		question = replaceValues(question, values);		
		
		//Set the question
		qinfo.setQuestion(question);
		qinfo.setRadioAnswerChoices(generateRadioAnswerChoises(answer, false));
		
		return qinfo;
	}
	
	
	public List<RadioData> generateRadioAnswerChoises(String answer, boolean decimalsAllowed) {
		//TODO when the answer has decimal digits, handle accordingly based on the variable decimalsAllowed
		List<RadioData> rData = new ArrayList<RadioData>();

		if (!decimalsAllowed) {
			List<Integer> answerChoices = RandomNumberGenerator.generateInts(answer.trim().length(), 4);
			rData.add(new RadioData(answer, answer));
			for (Integer intger : answerChoices) {
				String value = String.valueOf(intger.intValue());
				if(!value.equals(answer)) {
					RadioData data = new RadioData(value, value);
					rData.add(data);
				}
			}
		}
		else {
			//handle for decimal values
		}

		Collections.shuffle(rData);
		
		return rData;
	}

}
